/*
 * MailState.cpp
 *
 *  Created on: 2014. 4. 4.
 *      Author: mega
 */

#include "../User.h"
#include "../Command.h"
#include "../command/world/world.h"
#include "../NLogic/MailBox.h"
#include "../NLogic/Static.h"
#include "Static.h"
#include "MailState.h"

namespace ProjectZ { namespace NState {

MailState::MailState()
{
	_name = "MAILSTATE";

//	this->AddCommand(CMD_CS_REQ_REMOTE_CONTROL, ProjectZ::NCommand::Command::CS_REQ_REMOTE_CONTROL);
//	this->AddCommand(CMD_CS_SOCIAL_NEWS_COUNT, ProjectZ::NCommand::Command::CS_SOCIAL_NEWS_COUNT);
//	this->AddCommand(CMD_CS_SOCIAL_REQUEST_NEWS, ProjectZ::NCommand::Command::CS_SOCIAL_REQUEST_NEWS);
//	this->AddCommand(CMD_CS_REQ_SLOT_LIST, ProjectZ::NCommand::NWorld::CS_REQ_SLOT_LIST::OnExecute);
//	this->AddCommand(CMD_CS_REQ_USER_INFO_INVEN, ProjectZ::NCommand::NWorld::CS_REQ_USER_INFO_INVEN::OnExecute);
//	this->AddCommand(CMD_CS_REQ_USER_INFO_INVEN_BAG, ProjectZ::NCommand::NWorld::CS_REQ_USER_INFO_INVEN_BAG::OnExecute);
}

MailState::~MailState()
{
}


void
MailState::OnClose(User * pUser, bool bForceClose)
{
	pUser->SetDisconnectStatus(true);
}

void
MailState::OnResponse(::Database::Command * pCommand)
{
	User * pUser = static_cast<User *>(pCommand->GetObserver());

	if (true == pUser->GetDisconnectStatus())
	{
		pUser->OnDisconnect();
		return;
	}

	NLogic::MailBox * pMailBox = pUser->GetMailBox();

	if (DB_MAIL_DELETE_ALL == pCommand->GetCommandId())
	{
		if (NULL == pMailBox)
		{
			SendAckMailDeleteAll(pUser, ACK_UNKNOWN_ERROR);
			return;
		}

		if (0 != pCommand->GetErrorIdx())
		{
			SendAckMailDeleteAll(pUser, ACK_DB_ERROR);
			return;
		}

		std::vector<uint64_t> clsVector;

		auto iter = pMailBox->GetMailTree().begin();
		for (; iter != pMailBox->GetMailTree().end(); ++iter)
		{
			uint64_t mailSeq = iter->first;

			// 아이템이 첨부 되어 있는 메일은 삭제 하지 않는다.
			if (0 != iter->second->GetData().gift_status)
				continue;

			// 읽지 않은 메일은 삭제하지 않는다
			if (0 == iter->second->GetData().mail_status)
				continue;

			clsVector.push_back(mailSeq);

//			pMailBox->DelMailData(iter->first);
		}

		std::for_each(clsVector.begin(), clsVector.end(), [&](uint64_t seq){
			pMailBox->DelMailData(seq);
		});
		clsVector.clear();

		SendAckMailDeleteAll(pUser, ACK_OK);
	}
//	else if (DB_MAIL_COUNT)
//	{
//		if (NULL == pMailBox)
//		{
//			SendAckMailCount(pUser, ACK_UNKNOWN_ERROR);
//			return;
//		}
//
//		if (0 != pCommand->GetErrorIdx())
//		{
//			SendAckMailCount(pUser, ACK_DB_ERROR);
//			return;
//		}
//
//		pMailBox->Finalize();
//
//		int newMail	= 0;
//
//		while(pCommand->Next())
//		{
//			uint64_t mailSeq;
//			NLogic::MailBox::Mail::Data clsData;
//
//			pCommand->GetRecord() >> mailSeq;
//			pCommand->GetRecord() >> clsData.send_user_seq;
//			pCommand->GetRecord() >> clsData.send_user_nickname;
//			pCommand->GetRecord() >> clsData.send_user_imageurl;
//			pCommand->GetRecord() >> clsData.recv_user_seq;
//			pCommand->GetRecord() >> clsData.mail_status;
//			pCommand->GetRecord() >> clsData.gift_status;
//			pCommand->GetRecord() >> clsData.gift_vcindex;
//			pCommand->GetRecord() >> clsData.zen;
//			pCommand->GetRecord() >> clsData.gold;
//
//			pCommand->GetRecord() >> clsData.item.bag_type;
//			pCommand->GetRecord() >> clsData.item.bag_slot_number;
//			pCommand->GetRecord() >> clsData.item.quantity;
//			pCommand->GetRecord() >> clsData.item.class_type;
//			pCommand->GetRecord() >> clsData.item.sub_type;
//			pCommand->GetRecord() >> clsData.item.tid;
//			pCommand->GetRecord() >> clsData.item.iconidx;
//			pCommand->GetRecord() >> clsData.item.item_type;
//			pCommand->GetRecord() >> clsData.item.cur_duration;
//			pCommand->GetRecord() >> clsData.item.max_duration;
//			pCommand->GetRecord() >> clsData.item.set_type;
//			pCommand->GetRecord() >> clsData.item.non_identity;
//			pCommand->GetRecord() >> clsData.item.cur_refine_step;
//			pCommand->GetRecord() >> clsData.item.quality;
//			pCommand->GetRecord() >> clsData.item.level;
//
//			for (int i = 0; i < 7; ++i)
//			{
//				pCommand->GetRecord() >> clsData.item.eff_type[i];
//				pCommand->GetRecord() >> clsData.item.eff_pos[i];
//				pCommand->GetRecord() >> clsData.item.eff_value[i];
//			}
//
//			pCommand->GetRecord() >> clsData.item.open_upgrade_stone_slot;
//			pCommand->GetRecord() >> clsData.item.ability_enhance_rate;
//			pCommand->GetRecord() >> clsData.item.max_enhance_step;
//			pCommand->GetRecord() >> clsData.item.buy_use;
//			pCommand->GetRecord() >> clsData.item.evolve_step;
//			pCommand->GetRecord() >> clsData.item.evolve_max;
//			pCommand->GetRecord() >> clsData.item.evolve_point;
//			pCommand->GetRecord() >> clsData.item.evolve_percent;
//			pCommand->GetRecord() >> clsData.item.evolve_value;
//
//			pCommand->GetRecord() >> clsData.message;
//			pCommand->GetRecord() >> clsData.send_date;
//
//			NLogic::MailBox::Mail * pMail = NLogic::Static::InstancePtr()->GETMAILPOOL()->NEW();
//			pMail->Open_FromDatabase(mailSeq, clsData, pUser);
//			pMailBox->AddMailData(pMail);
//
//			if (0 == clsData.mail_status)
//				newMail++;
//		}
//
//		SendAckMailCount(pUser, ACK_OK, newMail);
//	}
}

void
MailState::SendAckMailDeleteAll(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_MAIL_DELETE_ALL);
	pUser->Send(pAck);

	pUser->SetState(NState::Static::Instance().MAINFRIENDLIST());
}

//void
//MailState::SendAckMailCount(User * pUser, int ack, int mail_count)
//{
//	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
//	*pAck << static_cast<int16_t>(ack);
//	*pAck << static_cast<uint16_t>(mail_count);
//	pAck->MakePacket(CMD_SC_GET_NEW_MAIL_COUNT_V2);
//	pUser->Send(pAck);
//
//	pUser->SetState(NState::Static::Instance().MAINFRIENDLIST());
//}


} /* namespace NState */ } /* namespace ProjectZ */
